/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */

/******************************************************************************\
 *          ____                _______    _____ _             _              *
 *         |  _ \              |__   __|  / ____| |           | |             *
 *         | |_) | ___  _ __ _ __ | | ___| (___ | |_ ___   ___| | __          *
 *         |  _ < / _ \| '__| '_ \| |/ _ \\___ \| __/ _ \ / __| |/ /          *
 *         | |_) | (_) | |  | | | | | (_) |___) | || (_) | (__|   <           *
 *         |____/ \___/|_|  |_| |_|_|\___/_____/ \__\___/ \___|_|\_\          *
 *                                                                            *
\******************************************************************************/

package app.controllers;

import app.core.UpdatedComp;
import app.events.eventobjects.VenteAddedEvent;
import app.events.eventobjects.VenteUpdatedEvent;
import app.events.eventobjects.VenteDeletedEvent;
import app.events.VenteListener;
import app.models.Factory;
import app.models.Lignevente;
import app.models.Reference;
import app.models.Vente;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.event.EventListenerList;

/**
 *
 * @author Ghost
 */
public class Cahier {
    
    private List<Vente> ventes;
    
    private static Vente actuelle;
    
    private EventListenerList listenerList = new EventListenerList();

    public Cahier() {
        ventes = new ArrayList<Vente>();
        prepareNewVente();
    }
    
    public static Vente prepareNewVente(){
        Vente v = new Vente();
        v.setClient(null);
        v.setLigneventeList(new ArrayList<Lignevente>());
        v.setBeauTemps(true);
        v.setDateV(Calendar.getInstance().getTime());
                
        return v;
    }
    
    public static void cancelVente(Vente v){
        for(Lignevente lv : v.getLigneventeList()){
            lv = Factory.getContext().merge(lv);
            Reference ref = Factory.getContext().merge(lv.getReference());
            int qte = ref.getStock() + lv.getQuantite();
            ref.setStock(qte);
            Factory.save(ref);
            Factory.remove(lv);
        }   
        Factory.remove(v);
    }

    /**
     * @deprecated de la merde
     * @return 
     */
    public Vente getActuelle() {
        return actuelle;
    }

    /**
     * @deprecated de la merde
     * @param actuelle 
     */
    public void setActuelle(Vente actuelle) {
        this.actuelle = actuelle;
    }
    
    public void saveVente(Vente v){
        Factory.save(v);
        fireVenteAdded(new VenteAddedEvent(v, this));
    }
    
    
    
    public void addVenteListener(VenteListener listener){
        listenerList.add(VenteListener.class, listener);
    }
    
    public void removeVenteListener(VenteListener listener){
        listenerList.remove(VenteListener.class, listener);
    }
    
    public void fireVenteAdded(VenteAddedEvent vae){
        Object[] listeners = listenerList.getListenerList();
        for (int i=0; i < listeners.length; i+=2) {
            if (listeners[i] == VenteListener.class) {
                ((VenteListener)listeners[i+1]).onVenteAdded(vae);
            }
        }
    }
    
    public void fireVenteChanged(VenteUpdatedEvent vce){
        Object[] listeners = listenerList.getListenerList();
        for (int i=0; i < listeners.length; i+=2) {
            if (listeners[i] == VenteListener.class) {
                ((VenteListener)listeners[i+1]).onVenteChanged(vce);
            }
        }
    }
    
    public void fireVenteDeleted(VenteDeletedEvent vde){
        Object[] listeners = listenerList.getListenerList();
        for (int i=0; i < listeners.length; i+=2) {
            if (listeners[i] == VenteListener.class) {
                ((VenteListener)listeners[i+1]).onVenteDeleted(vde);
            }
        }
    }
    
    
    
    
    /**
     * Chargement des ventes
     * @param comp m
     */
    public void load(UpdatedComp comp){
        comp.setCurrent(-1);
        List l = Factory.getContext().createNamedQuery("Vente.findAll").getResultList();
        comp.setMax(l.size());
        int i = 0;
        for(Object o : l)
        {
            comp.setCurrent(i++);
            Vente vente = (Vente)o;
            Factory.persist(vente);
            comp.setText("Chargement de de la vente " + vente.getId());
            ventes.add(vente);
            fireVenteAdded(new VenteAddedEvent(vente, this));
        }
    }
    
    
}
